package com.ssy.file.service.service.impl;

import com.ssy.file.service.service.FileUploadService;
import com.ssy.file.service.utils.AliyunOSSUtil;
import com.ssy.file.service.utils.FDFSClientUtil;
import com.ssy.lingxi.common.constant.CommonConstant;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.dto.response.FileItemResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class FileUploadServiceImpl implements FileUploadService {
    Logger log = LoggerFactory.getLogger(FileUploadServiceImpl.class);

    @Resource
    private AliyunOSSUtil aliyunOSSUtil;

    @Resource
    private FDFSClientUtil fastDFSClientUtil;

    /**
     * 文件上传
     *
     * @param request  文件
     * @param fileType 上传文件方式：1.阿里云  2.fastdfs
     * @author wrc
     * @date 2020/5/29
     */
    @Override
    public Wrapper<String> upload(MultipartFile request, Integer fileType) {
        String fileUrl = null;
        if (request == null || request.getOriginalFilename() == null) {
            return Wrapper.failService("无效文件");
        }

        if (request.getOriginalFilename().contains("/")) {
            return Wrapper.fail(ResponseCode.UPLOADED_FILE_ERROR);
        }

        if (CommonConstant.FileType.OSS_UPLOAD == fileType) {
            try {
                fileUrl = aliyunOSSUtil.upload(request.getInputStream(), request.getOriginalFilename());
            } catch (Exception e) {
                log.error("文件上传异常", e);
            }
        } else {
            fileUrl = fastDFSClientUtil.uploadFile(request);
        }
        return Wrapper.success(fileUrl);
    }

    /**
     * 文件上传
     *
     * @author wrc
     * @date 2020/9/23
     */
    @Override
    public Wrapper<FileItemResponse> uploadPrefix(MultipartFile file, Integer fileType, String prefix) {
        FileItemResponse fileItemResponse = new FileItemResponse();
        String fileUrl = null;
        if (file == null || file.getOriginalFilename() == null) {
            return Wrapper.failService("无效文件");
        }

        if (prefix.contains("/") || file.getOriginalFilename().contains("/")) {
            return Wrapper.fail(ResponseCode.UPLOADED_FILE_ERROR);
        }
        if (CommonConstant.FileType.OSS_UPLOAD == fileType) {
            try {
                fileItemResponse.setName(prefix + file.getOriginalFilename());
                fileUrl = aliyunOSSUtil.upload(file.getInputStream(), prefix + file.getOriginalFilename());
                fileItemResponse.setUrl(fileUrl);
                fileItemResponse.setThumbUrl(fileUrl);
            } catch (Exception e) {
                log.error("文件上传异常", e);
                return Wrapper.fail(ResponseCode.FILE_UPLOAD_ERROR);
            }
        } else {
            fileUrl = fastDFSClientUtil.uploadFile(file);
        }
        fileItemResponse.setUrl(fileUrl);
        return Wrapper.success(fileItemResponse);
    }

    /**
     * 文件删除
     *
     * @param fileUrl 文件Url
     * @author wrc
     * @date 2020/5/29
     */
    @Override
    public Wrapper<Void> delete(String fileUrl, Integer fileType) {
        if (CommonConstant.FileType.OSS_UPLOAD == fileType) {
            aliyunOSSUtil.deleteFile(fileUrl);
        } else {
            fastDFSClientUtil.delFile(fileUrl);
        }
        return Wrapper.success();
    }

    @Override
    public Wrapper<List<FileItemResponse>> uploadBatch(HttpServletRequest request, Integer fileType, String prefix) {
        prefix = StringUtils.hasLength(prefix) ? prefix : "";
        List<FileItemResponse> responses = new ArrayList<>();
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        if (CollectionUtils.isEmpty(files)) {
            return Wrapper.failService("无效文件");
        }

        if (CommonConstant.FileType.OSS_UPLOAD == fileType) {
            String fileUrl;
            for (MultipartFile file : files) {
                try {
                    if (prefix.contains("/") || Objects.requireNonNull(file.getOriginalFilename()).contains("/")) {
                        return Wrapper.fail(ResponseCode.UPLOADED_FILE_ERROR);
                    }
                    FileItemResponse fileItemResponse = new FileItemResponse();
                    fileItemResponse.setName(prefix + file.getOriginalFilename());
                    fileUrl = aliyunOSSUtil.upload(file.getInputStream(), prefix + file.getOriginalFilename());
                    fileItemResponse.setUrl(fileUrl);
                    fileItemResponse.setThumbUrl(fileUrl);
                    responses.add(fileItemResponse);
                } catch (Exception e) {
                    log.error("文件上传异常", e);
                }
            }
        }
        return Wrapper.success(responses);
    }
}
